package com.xxx.video.dynamic.activitys;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;

import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.ConcatAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.gms.tasks.OnSuccessListener;
import com.xxx.video.R;
import com.xxx.video.commen.activitys.BaseActivity;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.utils.LocationUtils;
import com.xxx.video.commen.utils.StatusBarUtil;
import com.xxx.video.commen.views.recyclerviews.RefreshRecyclerView;
import com.xxx.video.dynamic.adapters.MyFriendListBottomAdapter;
import com.xxx.video.dynamic.adapters.MyFriendListTopAdapter;
import com.xxx.video.dynamic.adapters.PeopleNearbyAdapter;
import com.xxx.video.dynamic.adapters.RecommendFriendAdapter;
import com.xxx.video.dynamic.beans.FriendListBean;
import com.xxx.video.dynamic.presenters.IMyFriendView;
import com.xxx.video.dynamic.presenters.MyFriendPresenter;

import butterknife.BindView;

public class MyFriendActivity extends BaseActivity<MyFriendPresenter> implements IMyFriendView, RefreshRecyclerView.OnRecyclerViewRefreshOrLoadMoreListener {
    @BindView(R.id.rcv_my_friend_list)
    RefreshRecyclerView rcvMyFriendList;
    private ConcatAdapter mConcatAdapter;
    /**
     * page
     */
    private int page = 1;
    /**
     * friend list adapter
     */
    private MyFriendListBottomAdapter mMyFriendListBottomAdapter;
    /**
     * location
     */
    private Location mLocation;
    private RecommendFriendAdapter mRecommendFriendAdapter;
    private PeopleNearbyAdapter mPeopleNearbyAdapter;

    @Override
    public void initToolbarData() {
        super.initToolbarData();
        showActionBar();
        StatusBarUtil.getInstance().setStatusBarTransparent(this, true);
        setIsShowActionbarLine(true);
        setActionBarTitle(R.string.title_my_friend_activity);
    }

    @Override
    protected MyFriendPresenter createPresenter() {
        return new MyFriendPresenter(this, this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.actiivty_my_friend;
    }

    @Override
    public void initData(Bundle extras) {
        showRootLayoutStatus(1);
        initList();
        initMyFriendTopList();
        getPresenter().recommendFriends();
        getPresenter().getFriendsList(page);
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @Override
    public void initList() {
        rcvMyFriendList.setLayoutManager(new LinearLayoutManager(this));
        mConcatAdapter = new ConcatAdapter();
        rcvMyFriendList.setAdapter(mConcatAdapter);
        rcvMyFriendList.setOnRecyclerViewRefreshOrLoadMoreListener(this);
        /*
         * is have location permission
         */
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            /*
             * get location
             */
            initLocation();
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            }
        }
    }

    @Override
    public void initMyFriendTopList() {
        MyFriendListTopAdapter myFriendListTopAdapter = new MyFriendListTopAdapter(this);
        mConcatAdapter.addAdapter(0, myFriendListTopAdapter);
        myFriendListTopAdapter.setOnItemClickListener((o, position, type) -> {
            switch (type) {
                case 1:
                    startAddContactsFriendsActivity();
                    break;
                case 2:
                    startGetFriendRequestActivity();
                    break;
                case 3:
                    startSendFriendRequestActivity();
                    break;
                case 4:
                    startFindFriendsActivity();
                    break;
                default:
                    break;
            }
        });
    }

    @Override
    public void initPeopleNearbyList(FriendListBean.PageBean pageBean) {
        if (ListUtils.isEmpty(pageBean.list)) {
            return;
        }
        mPeopleNearbyAdapter = new PeopleNearbyAdapter(this);
        mConcatAdapter.addAdapter(1, mPeopleNearbyAdapter);
        mPeopleNearbyAdapter.setFriendListData(pageBean.list);
        mPeopleNearbyAdapter.setOnItemClickListener((BaseRecycleViewAdapter.OnItemClickListener<FriendListBean.PageBean.ListBean>) (listBean, position, type) -> {
            if (type == 1) {
                startPeopleNearbyActivity();
            } else if (type == 0) {
                startPersonInfoActivity(listBean.userId);
            } else if (type == 3) {
                getPresenter().addFriends(listBean, position, 1);

            }
        });
    }

    @Override
    public void initLocation() {
        LocationUtils.getLocation(this, (OnSuccessListener<Location>) location -> {
            if (location != null) {
                mLocation = location;
                getPresenter().nearbyUser(String.valueOf(1), String.valueOf(location.getLongitude()), String.valueOf(location.getLatitude()));
            }
        });
    }

    @Override
    public void initRecommendFriendList(FriendListBean.PageBean pageBean) {
        if (ListUtils.isEmpty(pageBean.list)) {
            return;
        }
        mRecommendFriendAdapter = new RecommendFriendAdapter(this);
        if (mConcatAdapter.getAdapters().size() == 0) {
            mConcatAdapter.addAdapter(mRecommendFriendAdapter);
        } else if (mConcatAdapter.getAdapters().size() == 1) {
            if (mConcatAdapter.getAdapters().get(0) instanceof PeopleNearbyAdapter) {
                mConcatAdapter.addAdapter(mRecommendFriendAdapter);
            } else if (mConcatAdapter.getAdapters().get(0) instanceof MyFriendListTopAdapter) {
                mConcatAdapter.addAdapter(mRecommendFriendAdapter);
            }
        } else if (mConcatAdapter.getAdapters().size() == 2) {
            mConcatAdapter.addAdapter(mRecommendFriendAdapter);
        }
        mConcatAdapter.addAdapter(mRecommendFriendAdapter);
        mRecommendFriendAdapter.setFriendListData(pageBean.list);
        mRecommendFriendAdapter.setOnItemClickListener((BaseRecycleViewAdapter.OnItemClickListener<FriendListBean.PageBean.ListBean>) (listBean, position, type) -> {
            if (type == 1) {
                startRecommendFriendActivity();
            } else if (type == 0) {
                startPersonInfoActivity(listBean.userId);
            } else if (type == 3) {
                getPresenter().addFriends(listBean, position, 2);
            }
        });
    }

    @Override
    public void startRecommendFriendActivity() {
        intent2Activity(RecommendFriendActivity.class);
    }

    @Override
    public void startPeopleNearbyActivity() {
        intent2Activity(PeopleNearbyActivity.class);
    }

    @Override
    public void startGetFriendRequestActivity() {
        intent2Activity(MyGetFriendRequestActivity.class);
    }

    @Override
    public void startSendFriendRequestActivity() {
        intent2Activity(MySendFriendRequestActivity.class);
    }

    @Override
    public void startAddContactsFriendsActivity() {
        intent2Activity(AddContactsFriendsActivity.class);
    }

    @Override
    public void startFindFriendsActivity() {
        intent2Activity(FindFriendActivity.class);
    }

    @Override
    public void startPersonInfoActivity(String userId) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", userId);
        intent2Activity(PersonInfoActivity.class, bundle);
    }

    @Override
    public void initData2View(FriendListBean.PageBean pageBean) {
        rcvMyFriendList.resetState();
        if (mMyFriendListBottomAdapter == null) {
            mMyFriendListBottomAdapter = new MyFriendListBottomAdapter(this);
            mConcatAdapter.addAdapter(mMyFriendListBottomAdapter);
            mMyFriendListBottomAdapter.setOnItemClickListener((BaseRecycleViewAdapter.OnItemClickListener<FriendListBean.PageBean.ListBean>) (listBean, position, type) -> {
                startPersonInfoActivity(listBean.userId);
            });
        }
        if (page == 1) {
            mMyFriendListBottomAdapter.setData(pageBean.list);
        } else {
            mMyFriendListBottomAdapter.addData(pageBean.list);
        }
    }

    @Override
    public void addFriendSuccess(int position, int type) {
        if (type == 1) {
            if (mPeopleNearbyAdapter != null) {
                mPeopleNearbyAdapter.addFriendSuccess(position);
            }
        } else if (type == 2) {
            if (mRecommendFriendAdapter != null) {
                mRecommendFriendAdapter.addFriendSuccess(position);
            }
        }
    }

    @Override
    public void onRecyclerviewRefresh() {
        page = 1;
        getPresenter().getFriendsList(page);
    }

    @Override
    public void onRecyclerviewLoadMore() {
        page++;
        getPresenter().getFriendsList(page);
    }
}
